package com.web.servlet;

import com.web.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置请求数据字符编码
        req.setCharacterEncoding("UTF-8");
        //获取用户表单数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //实例化业务员
        UserService userService = new UserService();
        if(userService.login(username,password)){
            //重定向
            resp.sendRedirect("/index.jsp");
        }else{
            //绑定错误信息提示
            req.setAttribute("msg","账户或者密码错误!");
            //转发
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }
}
